import { login, logout, getInfo } from '@/http/api/myHttp'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { Message } from 'element-ui'

const getDefaultState = () => {
  return {
    token: getToken(), //刷新页面时，会从本地拿取token
    userInfo: {
      userEmail: '',
      userUid: '',
      orgCode: '',
      userName: '',
      mobilePhone: ''
    },
    perms: [], // 用户权限标识集合
    btnData: [] //用户的界面中可点击按钮
  }
}
export default {
  state: getDefaultState(),
  getters: {
    getUserInfo(state) {
      return state.userInfo
    }
  },
  mutations: {
    RESET_STATE: (state) => {
      Object.assign(state, getDefaultState())
    },
    setUserInfo(state, params) {
      state.userInfo = params
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    setPerms(state, perms) {
      // 用户权限标识集合
      state.perms = perms
    },
    setBtnData(state, perms) {
      // 用户权限标识集合
      state.btnData = perms
    }
  },
  actions: {
    // 登录
    login({ commit }, userInfo) {
      const { username, password } = userInfo
      return new Promise((resolve, reject) => {
        login({ username: username.trim(), password: password })
          .then((response) => {
            if (response.code == 0) {
              const { data } = response
              commit('SET_TOKEN', data.token)
              setToken(data.token)
              Message.success('登录成功！')
            }
            resolve(response)
          })
          .catch((error) => {
            Message.error('系统错误！')
            reject(error)
          })
      })
    },
    // 获取用户信息
    getInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token)
          .then((response) => {
            const { data } = response
            if (!data) {
              return reject('验证失败，请重新登录。')
            }
            const { userEmail, userUid, orgCode, userName, mobilePhone } = data
            commit('setUserInfo', {
              userEmail,
              userUid,
              orgCode,
              userName,
              mobilePhone
            })
            resolve(data)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 退出
    logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        // removeToken() // must remove  token  first
        // resetRouter()
        // commit('RESET_STATE')
        // resolve()
        //******************************************* */
        logout()
          .then(() => {
            removeToken() // must remove  token  first
            resetRouter()
            commit('RESET_STATE')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 清除token
    resetToken({ commit }) {
      return new Promise((resolve) => {
        removeToken() // must remove  token  first
        commit('RESET_STATE')
        resolve()
      })
    }
  }
}
